package org.linxiangyu.juanorm;

import android.util.Log;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Author: linxiangyu
 * Date:   13-11-8
 * Time:   上午10:31
 */
public class Model {


    Map<String, Field> fields;





    public Map<String, Field> getClassField() {

        Map<String, Field> fields = new HashMap<String, Field>();
        getAllFields(fields, getClass());

        return fields;
    }

    private static Map<String, Field> getAllFields(Map<String, Field> fields, Class<?> myClass) {
        for (Field field : myClass.getDeclaredFields()) {
            fields.put(field.getName(), field);
        }

        if (myClass.getSuperclass() != null) {
            fields = getAllFields(fields, myClass.getSuperclass());
        }

        return fields;
    }

    public void printAllFields(){
        fields = getClassField();
        Log.d("JuanORM", fields.toString());


        printMap(getClassField());

    }

    public static void printMap(Map mp) {
        Iterator it = mp.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            if (JuanORM.INSTANCE.isDebug)
                Log.d(JuanORM.INSTANCE.TAG, pairs.getKey() + " = " + pairs.getValue().getClass());
            it.remove(); // avoids a ConcurrentModificationException
        }
    }

}